package com.ysj.demo.accompanynetwork.fragment;

import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.OrientationHelper;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.jwenfeng.library.pulltorefresh.BaseRefreshListener;
import com.jwenfeng.library.pulltorefresh.PullToRefreshLayout;
import com.ysj.demo.accompanynetwork.R;
import com.ysj.demo.accompanynetwork.activity.PublishActivity;
import com.ysj.demo.accompanynetwork.adapter.HomeAdapter;
import com.ysj.demo.accompanynetwork.bean.ConsumeIntegralBean;
import com.ysj.demo.accompanynetwork.bean.PartnerPost;
import com.ysj.demo.accompanynetwork.bean.RestRsp;
import com.ysj.demo.accompanynetwork.mvp.presenter.RentYourselfPresenter;
import com.ysj.demo.accompanynetwork.mvp.view.RentYourselfView;
import com.ysj.demo.accompanynetwork.service.bean.page.Page;
import com.ysj.demo.accompanynetwork.util.SpUtil;
import com.ysj.demo.accompanynetwork.util.StatusBarUtil;
import com.ysj.demo.accompanynetwork.util.ToolUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * Created by 80530 on 2018/8/6.
 */

public class RentYourselfFragment extends Fragment implements RentYourselfView, View.OnClickListener {
    private RentYourselfPresenter presenter;
    private View view;
    private TextView titleTv, rightTv;
    private PullToRefreshLayout pullToRefreshLayout;
    private RecyclerView recyclerView;
    private HomeAdapter adapter;
    private List<ConsumeIntegralBean> list;

    private String userId;

    public RentYourselfFragment() {
        // Required empty public constructor
    }


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_rent_your_self_layout, container, false);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        userId = (String) SpUtil.getParam(getActivity(), SpUtil.USER_ID, "");
        titleTv = view.findViewById(R.id.top_tv);
        titleTv.setText(getResources().getString(R.string.rent_myself));
        rightTv = view.findViewById(R.id.top_right_tv);
        rightTv.setVisibility(View.VISIBLE);
        rightTv.setOnClickListener(this);

        initView();
        presenter.getList(userId);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.top_right_tv:
                Intent intent = new Intent(getActivity(), PublishActivity.class);
                intent.putExtra("TYPE", 1);
                startActivity(intent);
                break;
        }
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);

    }

    @Override
    public void onDetach() {
        super.onDetach();

    }

    @Override
    public void onResultSuccess(RestRsp<Page<PartnerPost>> data) {
        ToolUtil.ToastShow(getActivity().getApplication(), "数量=" + data.getData().getRecords().size());
    }

    @Override
    public void onFailListener(String msg) {
        ToolUtil.ToastShow(getActivity().getApplication(), "网络访问失败");
    }

    private void initView() {
        presenter = new RentYourselfPresenter();
        presenter.bind(this);
        list = new ArrayList<>();
        recyclerView = view.findViewById(R.id.rec_lv);
        pullToRefreshLayout = view.findViewById(R.id.activity_main);
        final GridLayoutManager layoutManager = new GridLayoutManager(getContext(), 2);

        adapter = new HomeAdapter(getContext(), list);

        //设置布局管理器
        recyclerView.setLayoutManager(layoutManager);
        //设置为垂直布局，这也是默认的
        layoutManager.setOrientation(OrientationHelper.VERTICAL);
        //设置Adapter
        recyclerView.setAdapter(adapter);
//        //设置分割线
//        //添加自定义分割线
//        DividerItemDecoration divider = new DividerItemDecoration(this, DividerItemDecoration.VERTICAL);
//        divider.setDrawable(ContextCompat.getDrawable(this, R.drawable.custom_divider));
//        recyclerView.addItemDecoration(divider);
        //设置增加或删除条目的动画
        recyclerView.setItemAnimator(new DefaultItemAnimator());

//        initData();
        pullToRefreshLayout.setRefreshListener(new BaseRefreshListener() {
            @Override
            public void refresh() {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        ArrayList<ConsumeIntegralBean> mList = new ArrayList<>();
                        ConsumeIntegralBean storeInfo1 =
                                new ConsumeIntegralBean(1, 10, "2018-10-1", "交易扣除");
                        ConsumeIntegralBean storeInfo2 =
                                new ConsumeIntegralBean(2, 10, "2018-10-2", "充值成功");
                        ConsumeIntegralBean storeInfo3 =
                                new ConsumeIntegralBean(1, 10, "2018-10-3", "交易扣除");
                        ConsumeIntegralBean storeInfo4 =
                                new ConsumeIntegralBean(1, 10, "2018-10-4", "交易扣除");
                        ConsumeIntegralBean storeInfo5 =
                                new ConsumeIntegralBean(2, 10, "2018-10-5", "充值成功");
                        ConsumeIntegralBean storeInfo6 =
                                new ConsumeIntegralBean(1, 10, "2018-10-6", "交易扣除");
                        mList.add(storeInfo1);
                        mList.add(storeInfo2);
//                        mList.add(storeInfo3);
//                        mList.add(storeInfo4);
//                        mList.add(storeInfo5);
//                        mList.add(storeInfo6);
                        adapter.clearList();
                        adapter.addList(mList);
                        // 结束刷新
                        pullToRefreshLayout.finishRefresh();
                    }
                }, 2000);
            }

            @Override
            public void loadMore() {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        LatestArticleTask task = new LatestArticleTask();
                        task.execute();
                        // 结束加载更多
                        pullToRefreshLayout.finishLoadMore();
                    }
                }, 2000);
            }
        });

        ArrayList<ConsumeIntegralBean> mList = new ArrayList<>();
        ConsumeIntegralBean storeInfo1 =
                new ConsumeIntegralBean(1, 10, "2018-10-1", "交易扣除");
        ConsumeIntegralBean storeInfo2 =
                new ConsumeIntegralBean(2, 10, "2018-10-2", "充值成功");
        ConsumeIntegralBean storeInfo3 =
                new ConsumeIntegralBean(1, 10, "2018-10-3", "交易扣除");
        ConsumeIntegralBean storeInfo4 =
                new ConsumeIntegralBean(1, 10, "2018-10-4", "交易扣除");
        ConsumeIntegralBean storeInfo5 =
                new ConsumeIntegralBean(2, 10, "2018-10-5", "充值成功");
        ConsumeIntegralBean storeInfo6 =
                new ConsumeIntegralBean(1, 10, "2018-10-6", "交易扣除");
        mList.add(storeInfo1);
        mList.add(storeInfo2);
//                        mList.add(storeInfo3);
//                        mList.add(storeInfo4);
//                        mList.add(storeInfo5);
//                        mList.add(storeInfo6);
        adapter.clearList();
        adapter.addList(mList);

    }

    class LatestArticleTask extends AsyncTask<String, Void, List<ConsumeIntegralBean>> {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }

        @Override
        protected List<ConsumeIntegralBean> doInBackground(String... params) {
            ArrayList<ConsumeIntegralBean> mList = new ArrayList<>();
            ConsumeIntegralBean storeInfo1 =
                    new ConsumeIntegralBean(1, 10, "2018-10-1", "交易扣除");
            ConsumeIntegralBean storeInfo2 =
                    new ConsumeIntegralBean(2, 10, "2018-10-2", "充值成功");
            ConsumeIntegralBean storeInfo3 =
                    new ConsumeIntegralBean(1, 10, "2018-10-3", "交易扣除");
            ConsumeIntegralBean storeInfo4 =
                    new ConsumeIntegralBean(1, 10, "2018-10-4", "交易扣除");
            ConsumeIntegralBean storeInfo5 =
                    new ConsumeIntegralBean(2, 10, "2018-10-5", "充值成功");
            ConsumeIntegralBean storeInfo6 =
                    new ConsumeIntegralBean(1, 10, "2018-10-6", "交易扣除");

            Random random = new Random();
            int index = random.nextInt(6) + 1;
            switch (index) {
                case 1:
                    mList.add(storeInfo1);
                    break;
                case 2:
                    mList.add(storeInfo1);
                    mList.add(storeInfo2);
                    break;
                case 3:
                    mList.add(storeInfo1);
                    mList.add(storeInfo2);
                    mList.add(storeInfo3);
                    break;
                case 4:
                    mList.add(storeInfo1);
                    mList.add(storeInfo2);
                    mList.add(storeInfo3);
                    mList.add(storeInfo4);
                    break;
                case 5:
                    mList.add(storeInfo1);
                    mList.add(storeInfo2);
                    mList.add(storeInfo3);
                    mList.add(storeInfo4);
                    mList.add(storeInfo5);
                    break;
                case 6:
                    mList.add(storeInfo1);
                    mList.add(storeInfo2);
                    mList.add(storeInfo3);
                    mList.add(storeInfo4);
                    mList.add(storeInfo5);
                    mList.add(storeInfo6);
                    break;
            }
            return mList;
        }

        @Override
        protected void onPostExecute(List<ConsumeIntegralBean> data) {
            super.onPostExecute(data);
            adapter.addList(data);

            Toast.makeText(getActivity().getApplicationContext(), "刷新成功" + list.size(), Toast.LENGTH_SHORT).show();
        }
    }
}
